package com.zy.common.utils;

import lombok.*;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * Copyright © 2018 CHJ Automotive
 *
 * @author tonfu.chia
 * @since 2019/2/20 下午7:33
 */
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Data
@NoArgsConstructor
public class OkHttpResp implements Serializable {

    private static final Long serialVersionUID = -4413748860584534649L;

    private int code;

    private String msg;

    private String bodyString;

    public static OkHttpResp from(HttpStatus httpStatus) {
        OkHttpResp okHttpResp = OkHttpResp.builder()
                .code(httpStatus.value())
                .msg(httpStatus.getReasonPhrase())
                .build();
        return okHttpResp;
    }

    public static OkHttpResp from(HttpStatus httpStatus, String bodyString) {
        OkHttpResp okHttpResp = OkHttpResp.builder()
                .code(httpStatus.value())
                .msg(httpStatus.getReasonPhrase())
                .bodyString(bodyString)
                .build();
        return okHttpResp;
    }

    public boolean isSuccess() {
        if (HttpStatus.OK.value() == code) {
            return true;
        }
        return false;
    }
}
